package com.saicyc.demo.config;

import com.zaxxer.hikari.HikariDataSource;
import org.flywaydb.core.Flyway;
import org.flywaydb.core.api.Location;
import org.flywaydb.core.api.configuration.ClassicConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;

import java.nio.charset.StandardCharsets;


@Profile("flyway")
@Configuration
public class FlywayConfiguration {

    @Bean
    public Flyway flyway(HikariDataSource hikariDataSource){
        ClassicConfiguration configuration = new ClassicConfiguration();
        configuration.setDataSource(hikariDataSource);
        configuration.setEncoding(StandardCharsets.UTF_8);
        configuration.setTable("schema_version");
        configuration.setLocations(new Location("db/migration"));
        Flyway flyway = new Flyway(configuration);
        flyway.migrate();
        return flyway;
    }
}

